package com.example.forum2.services.impl;

import com.example.forum2.common.AppResult;
import com.example.forum2.common.ResultCode;
import com.example.forum2.dao.MessageMapper;
import com.example.forum2.exception.ApplicationException;
import com.example.forum2.model.Message;
import com.example.forum2.model.User;
import com.example.forum2.services.IMessageServcie;
import com.example.forum2.services.IUserService;
import com.example.forum2.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageServcie {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private IUserService iUserService;
    @Override
    public void create(Message message) {
//   首先还是进行非空校验
   if(message==null||message.getPostUserId()==null||message.getReceiveUserId()==null|| StringUtil.isEmpty(message.getContent())){
       log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
       throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
   }
//   检验接收者是否存在
        User user=iUserService.selectById(message.getReceiveUserId());
   if(user==null||user.getDeleteState()==1){
       log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
       throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
   }
//   发送者是存在被禁言
        User user2=iUserService.selectById(message.getPostUserId());
   if(user.getState()==1) {
       log.warn(ResultCode.FAILED_USER_BANNED.toString());
       throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
   }
//   设置默认值
        message.setState((byte)0);
        message.setDeleteState((byte)0);
        Date date=new Date();
        message.setUpdateTime(date);
        message.setCreateTime(date);
//        调用dao
        int row=messageMapper.insertSelective(message);
      if(row!=1){
          log.warn(ResultCode.FAILED.toString());
          throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
      }
    }

    @Override
    public Message selectById(Long id) {
        if(id<=0||id==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message=messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
//         收先还是进行非空校验
        if(receiveUserId==null||receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        调用dao层进行查询即可
       int count=messageMapper.selectUnreadCount(receiveUserId);
//        返回即可
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
//        非空校验
        if(receiveUserId==null||receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//       调用dao层
        List<Message> messages=messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, byte state) {
//        首秀还是对参数进行校验
        if (id == null || id <= 0 || state < 0 || state > 2) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//   构造新的对象
       Message message=new Message();
        message.setId(id);
        message.setState(state);
        Date date=new Date();
        message.setUpdateTime(date);
//        调用dao层
        int row=messageMapper.updateByPrimaryKeySelective(message);
        if(row!=1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void reply(Long repliedId, Message message) {
//        非空校验
        if(repliedId==null||repliedId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        检验站内信对应的状态
        Message existsmessage=messageMapper.selectByPrimaryKey(repliedId);
        if(existsmessage==null||existsmessage.getDeleteState()==1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));

        }
//        更新状态为已回复
        updateStateById(repliedId,(byte)2);
//        回复的内容写入数据库
        create(message);
    }
}
